package dts.com.mx.seguridad;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Component;

import dts.com.mx.HomeController;
import dts.com.mx.vo.User;

@Component
public class CustomAuthenticationProvider  implements AuthenticationProvider {

	private static final Logger logger = LoggerFactory.getLogger(CustomAuthenticationProvider.class);

	public Authentication authenticate(Authentication autentica)
			throws AuthenticationException {

		try{
		String principal = (String) autentica.getPrincipal();
		String password  = (String) autentica.getCredentials();
		
		List<User> userData =  getUserData();
		
		logger.info("------principal --" +  principal);
    	logger.info("------password ---" +  password);
		
		for (User user : userData){
			
			String userName = user.getUserName();
			String userPassword = user.getPassword();
			
			if (userName.equals(principal) && userPassword.equals(userPassword)){
				List<String> roles = user.getRoles();
				List<GrantedAuthority> authorities  = new ArrayList<GrantedAuthority>();
				for (String role : roles){
					authorities.add(new SimpleGrantedAuthority(role));
				}
				
				return new UsernamePasswordAuthenticationToken(userName, password ,  authorities);
			}
		}
		
		throw new BadCredentialsException("Usuario o password no concuerdan para el acceso");
		}catch (Exception e) {
			throw new BadCredentialsException(e.getMessage() , e);
		}
	}

	public boolean supports(Class<? extends Object > autenticacion) {
		
		return autenticacion.equals(UsernamePasswordAuthenticationToken.class);
	}
	
	private List<User> getUserData (){
		
		List<User> userDataList = new ArrayList<User>();
		
		
		List<String > roles = new ArrayList<String>();
		roles.add("ROL_SU");
		roles.add("ROL_ADMIN");
		roles.add("ROLE_USER");
		roles.add("ROLE_TONO");

		User user = new User();
		user.setPassword("ingmja22");
		user.setUserName("ingmja22");
		user.setRoles(roles);
		userDataList.add(user);
		
		user = new User();
		roles = new ArrayList<String>();
		roles.add("ROLE_USER");
		user.setPassword("ingmja23");
		user.setUserName("ingmja23");
		user.setRoles(roles);
		userDataList.add(user);
		
		user = new User();
		roles = new ArrayList<String>();
		roles.add("ROL_ADMIN");
		user.setPassword("ingmja24");
		user.setUserName("ingmja24");
		user.setRoles(roles);
		userDataList.add(user);
		
		
		
		return userDataList;
	
	
	}
	

}
